package com.boot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boot.entity.BaseResponse;
import com.boot.entity.TestEntity;
import com.boot.utils.OkHttpUtils;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: 用户控制层
 * @author: lh
 */
@RestController
@RequestMapping("/test")
public class TestController {

    AtomicInteger atomicIntegerForGet = new AtomicInteger();
    AtomicInteger atomicIntegerForPostJson = new AtomicInteger();
    AtomicInteger atomicIntegerForPostStr = new AtomicInteger();
    AtomicInteger atomicIntegerForPostForm = new AtomicInteger();
    AtomicInteger atomicInteger = new AtomicInteger();

    @GetMapping("/monitor")
    public Map<String, Object> monitor()
    {
        Map<String, Object> monitorRunInfo = null;
        try {
             monitorRunInfo = OkHttpUtils.builder().getMonitorRunInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return monitorRunInfo;
    }

//    @GetMapping("/beginTestGet")
//    public void beginTestGet()
//    {
//        try {
//            testOkhttpGet();
//            testOkhttpPostJson();
//            testOkhttpPostString();
//            testOkhttpPostMap();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    @GetMapping("/beginTestGet")
    public void beginTestGet() {
        try {
            int random = new Random().nextInt(6);
            if (random == 0) {
                testOkhttpGet();
            } else if (random == 1) {
                testOkhttpPostJson();
            } else if (random == 2) {
                testOkhttpPostString();
            } else if(random == 3) {
                testOkhttpPostMap();
            } else if (random == 4) {
                testOkhttpPostJson2();
            } else   {
                testOkhttpPostJson3();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void testOkhttpGet() throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put( "uuid", UUID.randomUUID().toString());
        map.put("method", "testOkhttpGet testGet");
        OkHttpUtils.HttpResult res = OkHttpUtils.builder()
                .url("http://localhost:8080/user/testGet")
                .get(map)
                .sync();
        System.out.println(JSON.toJSONString(res));
    }

    @GetMapping("/testGet")
    public String login(@RequestParam String uuid, @RequestParam String method)
    {
        BaseResponse response = new BaseResponse();
        atomicIntegerForGet.incrementAndGet();
        //System.out.println(uuid+method);
        return uuid+method;
    }

    @PostMapping("testPostJson")
    public String register(@RequestBody TestEntity entity)
    {
        BaseResponse response = new BaseResponse();
        if (entity != null)
        {
            response.setCode(200);
            response.setMsg("用户注册成功");
        } else {
            response.setCode(500);
            response.setMsg("注册失败，请检查后重试");
        }
        atomicIntegerForPostJson.incrementAndGet();
       // System.out.println(JSON.toJSONString(entity));
        return JSON.toJSONString(entity);
    }

    public void testOkhttpPostJson() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put( "uuid", UUID.randomUUID().toString());
        jsonObject.put("method", "testOkhttpPostJson testPostJson");
        OkHttpUtils.HttpResult res = OkHttpUtils.builder()
                .url("http://localhost:8080/user/testPostJson")
                .postJson(jsonObject)
                .sync();
        System.out.println(JSON.toJSONString(res));
    }
    public void testOkhttpPostJson2() {
        OkHttpUtils.TimeConfig timeConfig = new OkHttpUtils.TimeConfig(10,5,10);
        OkHttpUtils.ConnectionPoolConfig poolConfig = new OkHttpUtils.ConnectionPoolConfig(20,60);


        JSONObject jsonObject = new JSONObject();
        jsonObject.put( "uuid", UUID.randomUUID().toString());
        jsonObject.put("method", "testOkhttpPostJson2 testPostJson");
        OkHttpUtils.HttpResult res = OkHttpUtils.builder(timeConfig,null,poolConfig)
                .url("http://localhost:8080/user/testPostJson")
                .postJson(jsonObject)
                .sync();
        System.out.println(JSON.toJSONString(res));
    }

    public void testOkhttpPostJson3() {
        OkHttpUtils.TimeConfig timeConfig = new OkHttpUtils.TimeConfig(15,10,5);
        OkHttpUtils.ConnectionPoolConfig poolConfig = new OkHttpUtils.ConnectionPoolConfig(10,120);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put( "uuid", UUID.randomUUID().toString());
        jsonObject.put("method", "testOkhttpPostJson3  testPostJson");
        OkHttpUtils.HttpResult res = OkHttpUtils.builder(timeConfig,null,poolConfig)
                .url("http://localhost:8080/user/testPostJson")
                .postJson(jsonObject)
                .sync();
        System.out.println(JSON.toJSONString(res));
    }

    public void testOkhttpPostString() {
        String encodeString =" testOkhttpPostStringtestPostStr 辅导时间发了多少丰的沙发上"+UUID.randomUUID().toString();
        OkHttpUtils.HttpResult res = OkHttpUtils.builder()
                .url("http://localhost:8080/user/testPostStr")
                .postString(encodeString)
                .sync();
        System.out.println(JSON.toJSONString(res));
    }

    public void testOkhttpPostMap() throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put( "uuid", UUID.randomUUID().toString());
        map.put("method", "testOkhttpPostMap testPostForm");
        OkHttpUtils.HttpResult res = OkHttpUtils.builder()
                .url("http://localhost:8080/user/testPostForm")
                .postMap(map)
                .sync();
        System.out.println(JSON.toJSONString(res));
    }
}